//
//  ZMEmotionKeyBoard.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/8/1.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

//点击表情按钮对应的通知
let ClickEmoticonButtonNotificationName = "ClickEmoticonButtonNotificationName"

//点击删除按钮发送的通知
let ClickDeleteEmoticonButtonNotificationName = "ClickDeleteEmoticonButtonNotificationName"


class ZMEmotionKeyBoard: UIView {

    //懒加载toolbar
    fileprivate lazy var toolBar : ZMEmotionKeyBoarkToolBar = {
        
        let tool = ZMEmotionKeyBoarkToolBar()
        
        
        return tool
    }()
    
    
    //懒加载表情视图
    fileprivate lazy var emotionView:UICollectionView = {
        //定义布局对象
        let flowLayout = UICollectionViewFlowLayout()
        //定义视图的frame
        let frame = CGRect(origin: CGPoint.zero, size: CGSize(width: self.width, height: self.height - 35))
        let collectionview : UICollectionView = UICollectionView(frame: frame , collectionViewLayout: flowLayout)
        //设置背景色
        collectionview.backgroundColor = UIColor.white
        //设置itemsize的值
        flowLayout.itemSize = collectionview.size//CGSize(width: self.width / 7, height: (self.height-CGFloat(35)) / 3)
        //设置最先间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //滚动方向
        flowLayout.scrollDirection = .horizontal
        //隐藏滚动条
        collectionview.showsHorizontalScrollIndicator = false
        collectionview.showsVerticalScrollIndicator = false
        
        //使用分页
        collectionview.isPagingEnabled = true
        
        //设置数据源和代理
        collectionview.dataSource = self
        collectionview.delegate = self
    
        
        return collectionview
    }()
    //  加载最近镖旗
    func reloadRecentData() {
        
        let indexPath = IndexPath(item: 0, section: 0)
        emotionView.reloadItems(at: [indexPath])
    }
    
   //MARK:- 懒加载分页指示器
    fileprivate lazy var pageControl: UIPageControl = {
        
        //设置分页指示器的背景图片
        let pagecon = UIPageControl()
        pagecon.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pagecon.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "_pageImage")
        pagecon.hidesForSinglePage = true
        return pagecon
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:- pagecontrol 参数设置
    func setPageControlData(indexpath:IndexPath){
        pageControl.numberOfPages = ZMEmotiontools.sharedTools.allEmotionArray[indexpath.section].count
        pageControl.currentPage = indexpath.item
    }
    
    private func setupUI(){
        //self.backgroundColor = UIColor.orange
        //添加底部工具条
        self.addSubview(toolBar)
        //设置约束
        toolBar.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: toolBar, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: toolBar, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: toolBar, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: toolBar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 35))
        
        //添加表情视图
        self.addSubview(emotionView)
      //  emotionView.translatesAutoresizingMaskIntoConstraints = false
        
        print(emotionView.frame)
        
        //注册cell
        emotionView.register(ZMEmotionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        //添加分页指示器视图
        let indexpath = IndexPath(item: 0, section: 1)
        emotionView.scrollToItem(at: indexpath, at: UICollectionViewScrollPosition.left, animated: true)
        setPageControlData(indexpath: indexpath)
        addSubview(pageControl)
        pageControl.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(NSLayoutConstraint(item: pageControl, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: emotionView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: pageControl, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: emotionView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: pageControl, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 30))
        
        
        //  设置闭包
        toolBar.clickToolBarButtonClosure = {[unowned self] (type: EmotionKeyBoardToolBarButtonType) in
            let indexPath: IndexPath
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: 0)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: 1)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: 2)
            case .ikh:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: 3)
            }
            //  滚动到指定的indexPath
            self.emotionView.scrollToItem(at: indexPath, at: [], animated: false)
        }

        
        //添加通知监听,,监听表情按钮按下   跟新最近表情
        NotificationCenter.default.addObserver(self, selector: #selector(reloadRecentData), name: NSNotification.Name(ClickEmoticonButtonNotificationName), object: nil)
        
        
    }
}
//MARK:- 表情视图的数据原方法和代理方法
extension ZMEmotionKeyBoard: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return ZMEmotiontools.sharedTools.allEmotionArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ZMEmotiontools.sharedTools.allEmotionArray[section].count
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ZMEmotionViewCell
        
     // cell.backgroundColor = RandowColor()
        
        cell.indexPath = indexPath
        
        cell.emotionArray = ZMEmotiontools.sharedTools.allEmotionArray[indexPath.section][indexPath.row]
        
        
        
        cell.textString = "\(indexPath.section)-\(indexPath.item)"
       
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        //  获取水平方向内容中心点
        let contentCenterX = scrollView.width * 0.5 + scrollView.contentOffset.x
        //  获取垂直方向内容中心点
        let contentCenterY = scrollView.height * 0.5
        //  获取内容中心点
        let contentCenter = CGPoint(x: contentCenterX, y: contentCenterY)
        //  根据内容中点获取对于的indexPath
        if let indexPath = emotionView.indexPathForItem(at: contentCenter) {
            
            toolBar.selectButtonWith(buttonTag: indexPath.section)
            setPageControlData(indexpath: indexPath)
        }
        
        
    }
}





